package performance.jmeter;

import performance.BaseTpsTest;
import performance.ResourceLoadUtil;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 多客户端多线程测数据库性能测试 <br/>
 * 不同库同表
 *
 * @author yancheng11334
 *
 */
public class TpsExecuteTest extends BaseTpsTest {


    private static final String  STR_LONG_TEXT = "疯似的飞三番四复三番四复三番四复收发室圣克拉拉开发放到沙发上发撒旦发生发生发撒旦发撒发撒旦发撒发生飞洒发生几率就立即哦记录垃圾街基金经理疯似的飞三番四复三番四复三番四复收发室圣克拉拉开发放到沙发上发撒旦发生发生发撒旦发撒发撒旦发撒发生飞洒发生几率就立即哦记录垃圾街基金经理";
    private static final AtomicInteger insertCount = new AtomicInteger(0);

    /**
     * 测试一定时间内的插入SQL性能<br/>
     * 顺序生成主键
     * @throws Exception
     */
    public void doExe() throws Exception {
        Connection conn = null;
        PreparedStatement pStmt = null;
        try {
            //测试长连接(1个线程生命周期只创建一次连接)
            conn = getConnection();
            pStmt = conn.prepareStatement("insert into score(id,name,score,course) values(?,?,?,?)");
            long temp = (long)(Math.random()*500000);
            long id = insertCount.getAndIncrement();
            pStmt.setLong(1, id);
            pStmt.setString(2, "yc"+temp);
            pStmt.setInt(3, 60);
            pStmt.setString(4, temp+STR_LONG_TEXT);
            pStmt.executeUpdate();
        } finally {
            close(pStmt);
            close(conn);
        }
    }


    public void doBatchExe(int insertNum) throws SQLException {
        int count = 0;
        Connection conn = null;
        PreparedStatement pStmt = null;
        try {
            //测试长连接(1个线程生命周期只创建一次连接)
            conn = getConnection();
            pStmt = conn.prepareStatement("insert into score(id,name,score,course) values(?,?,?,?)");
            conn.setAutoCommit(false);
            int r=0;
            while ((r=insertCount.getAndIncrement()) < insertNum) {
                count++;
                long temp = (long)(Math.random()*500000);
                long id = r;
                pStmt.setLong(1, id);
                pStmt.setString(2, "yc"+temp);
                pStmt.setInt(3, 60);
                pStmt.setString(4, temp+STR_LONG_TEXT);
                pStmt.addBatch();
                if(count%1000==0){
                    pStmt.executeBatch();
                }
            }
            pStmt.executeBatch();
            conn.commit();
        } finally {
            close(pStmt);
            close(conn);
        }
    }

    public void doUpdate() throws Exception {
        Connection conn = null;
        PreparedStatement pStmt = null;
        try {
            //测试长连接(1个线程生命周期只创建一次连接)
            conn = getConnection();
            pStmt = conn.prepareStatement("update t_student set detail=? where id=?");
            long temp = (long)(Math.random()*100000);
            pStmt.setString(1, "detail-u");
            pStmt.setLong(2, temp);
            pStmt.executeUpdate();
        } finally {
            close(pStmt);
            close(conn);
        }
    }

}
